/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  javafx.application.Platform
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.scene.Node
 *  javafx.scene.Scene
 *  javafx.scene.control.Button
 *  javafx.scene.control.Label
 *  javafx.scene.control.Slider
 *  javafx.scene.effect.ColorAdjust
 *  javafx.scene.effect.Effect
 *  javafx.scene.image.Image
 *  javafx.scene.image.ImageView
 *  javafx.scene.input.InputEvent
 *  javafx.scene.input.MouseEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.scene.layout.BorderPane
 *  javafx.scene.layout.VBox
 *  javafx.scene.media.Media
 *  javafx.scene.media.MediaPlayer
 *  javafx.scene.media.MediaPlayer$Status
 *  javafx.scene.media.MediaView
 *  javafx.stage.Stage
 *  javafx.util.Duration
 */
package com.gys.controller.media;

import com.gys.controller.media.SimpleMediaPlayer;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.Effect;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.InputEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PlayerController {
    @FXML
    Button playBT;
    @FXML
    Button stopBT;
    @FXML
    Button maxBT;
    @FXML
    Button volumeBT;
    @FXML
    Label timeLB;
    @FXML
    Slider processSD;
    @FXML
    Slider volumeSD;
    @FXML
    MediaView mediaView;
    @FXML
    VBox controlBar;
    @FXML
    BorderPane mediaPane;
    @FXML
    AnchorPane anchorPane;
    private String playIcon = this.getClass().getResource("/image/media/play.png").toString();
    private String pauseIcon = this.getClass().getResource("/image/media/pause.png").toString();
    private String stopIcon = this.getClass().getResource("/image/media/stop.png").toString();
    private String volOffIcon = this.getClass().getResource("/image/media/volume_off.png").toString();
    private String volOnIcon = this.getClass().getResource("/image/media/volume_On.png").toString();
    private String maxIcon = this.getClass().getResource("/image/media/max.png").toString();
    private MediaPlayer mediaPlayer;
    private Media media;
    private String url;
    private boolean popup;
    private Scene scene;
    private boolean atEndOfMedia = false;
    private final boolean repeat = false;
    private double volumeValue;
    private Duration duration;
    private int mediaHeight;
    private int mediaWidth;
    private int currentHeight;
    private int currentWidth;

    public void setScene(Scene scene) {
        this.scene = scene;
    }

    public void initialize() {
        this.setIcon(this.playBT, this.playIcon, 25);
        this.setIcon(this.stopBT, this.stopIcon, 25);
        this.setIcon(this.volumeBT, this.volOnIcon, 15);
        this.setIcon(this.maxBT, this.maxIcon, 25);
    }

    public void start(String url, boolean popup, int width, int height) {
        this.url = url;
        this.popup = popup;
        this.media = new Media(url);
        this.mediaPlayer = new MediaPlayer(this.media);
        this.mediaView.setMediaPlayer(this.mediaPlayer);
        this.setMediaPlayer(width, height);
        this.setMediaViewOnClick();
        this.setPlayButton();
        this.setStopButton();
        this.setVolumeButton();
        this.setVolumeSD();
        this.setProcessSlider();
        this.setMaximizeButton();
    }

    public void setMediaPlayer(final int width, final int height) {
        this.mediaPlayer.setCycleCount(1);
        this.mediaPlayer.setOnReady(new Runnable(){

            @Override
            public void run() {
                PlayerController.this.duration = PlayerController.this.mediaPlayer.getMedia().getDuration();
                PlayerController.this.volumeValue = PlayerController.this.mediaPlayer.getVolume();
                PlayerController.this.mediaHeight = PlayerController.this.media.getHeight();
                PlayerController.this.mediaWidth = PlayerController.this.media.getWidth();
                PlayerController.this.setSize(width, height);
                if (PlayerController.this.scene != null) {
                    PlayerController.this.scene.widthProperty().addListener((ChangeListener)new ChangeListener<Number>(){

                        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                            PlayerController.this.setSize(newValue.intValue(), PlayerController.this.currentHeight);
                        }
                    });
                    PlayerController.this.scene.heightProperty().addListener((ChangeListener)new ChangeListener<Number>(){

                        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                            PlayerController.this.setSize(PlayerController.this.currentWidth, newValue.intValue());
                        }
                    });
                }
                EventHandler<InputEvent> onScreen = new EventHandler<InputEvent>(){

                    public void handle(InputEvent event) {
                        PlayerController.this.controlBar.setVisible(true);
                    }
                };
                EventHandler<InputEvent> offScreen = new EventHandler<InputEvent>(){

                    public void handle(InputEvent event) {
                        PlayerController.this.controlBar.setVisible(false);
                    }
                };
                if (PlayerController.this.scene != null && PlayerController.this.popup) {
                    ((Stage)PlayerController.this.scene.getWindow()).fullScreenProperty().addListener((ChangeListener)new ChangeListener<Boolean>(){
                        final EventHandler val_onScreen;
                        final EventHandler val_offScreen;
                        {
                            this.val_onScreen = onScreen;
                            this.val_offScreen = offScreen;
                        }

                        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                            if (newValue.booleanValue()) {
                                PlayerController.this.controlBar.setVisible(false);
                                PlayerController.this.mediaPane.addEventHandler(MouseEvent.MOUSE_CLICKED, this.val_onScreen);
                                PlayerController.this.controlBar.addEventHandler(MouseEvent.MOUSE_EXITED, this.val_offScreen);
                            } else {
                                PlayerController.this.controlBar.setVisible(true);
                                PlayerController.this.mediaPane.removeEventHandler(MouseEvent.MOUSE_CLICKED, this.val_onScreen);
                                PlayerController.this.controlBar.removeEventHandler(MouseEvent.MOUSE_EXITED, this.val_offScreen);
                            }
                        }
                    });
                }
                PlayerController.this.updateValues();
            }
        });
        this.mediaPlayer.currentTimeProperty().addListener((ChangeListener)new ChangeListener<Duration>(){

            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                PlayerController.this.updateValues();
            }
        });
    }

    private void setMediaViewOnClick() {
        this.mediaView.setOnMouseClicked(event -> {
            if (this.media == null) {
                return;
            }
            MediaPlayer.Status status = this.mediaPlayer.getStatus();
            if (status == MediaPlayer.Status.UNKNOWN || status == MediaPlayer.Status.HALTED) {
                return;
            }
            if (status == MediaPlayer.Status.PAUSED || status == MediaPlayer.Status.READY || status == MediaPlayer.Status.STOPPED) {
                if (this.atEndOfMedia) {
                    this.mediaPlayer.seek(this.mediaPlayer.getStartTime());
                    this.atEndOfMedia = false;
                }
                this.mediaPlayer.play();
                this.setIcon(this.playBT, this.pauseIcon, 25);
            } else {
                this.mediaPlayer.pause();
                this.setIcon(this.playBT, this.playIcon, 25);
            }
        });
    }

    private void setPlayButton() {
        this.playBT.setOnAction(e -> {
            if (this.media == null) {
                return;
            }
            MediaPlayer.Status status = this.mediaPlayer.getStatus();
            if (status == MediaPlayer.Status.UNKNOWN || status == MediaPlayer.Status.HALTED) {
                return;
            }
            if (status == MediaPlayer.Status.PAUSED || status == MediaPlayer.Status.READY || status == MediaPlayer.Status.STOPPED) {
                if (this.atEndOfMedia) {
                    this.mediaPlayer.seek(this.mediaPlayer.getStartTime());
                    this.atEndOfMedia = false;
                }
                this.mediaPlayer.play();
                this.setIcon(this.playBT, this.pauseIcon, 25);
            } else {
                this.mediaPlayer.pause();
                this.setIcon(this.playBT, this.playIcon, 25);
            }
        });
    }

    private void setStopButton() {
        this.stopBT.setOnAction(e -> {
            if (this.media == null) {
                return;
            }
            this.mediaPlayer.stop();
            this.setIcon(this.playBT, this.playIcon, 25);
        });
    }

    private void setProcessSlider() {
        this.processSD.valueProperty().addListener((ChangeListener)new ChangeListener<Number>(){

            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (PlayerController.this.processSD.isValueChanging()) {
                    PlayerController.this.mediaPlayer.seek(PlayerController.this.duration.multiply(PlayerController.this.processSD.getValue() / 100.0));
                }
            }
        });
    }

    public void setMaximizeButton() {
        this.maxBT.setOnAction(e -> {
            if (this.popup) {
                ((Stage)this.scene.getWindow()).setFullScreen(true);
            } else {
                this.mediaPlayer.pause();
                this.setIcon(this.playBT, this.pauseIcon, 25);
                SimpleMediaPlayer player = SimpleMediaPlayer.popup(this.url);
                player.getController().getMediaPlayer().seek(this.mediaPlayer.getCurrentTime());
            }
        });
    }

    private void setVolumeButton() {
        this.volumeBT.setOnAction(e -> {
            if (this.media == null) {
                return;
            }
            if (this.mediaPlayer.getVolume() > 0.0) {
                this.volumeValue = this.mediaPlayer.getVolume();
                this.volumeSD.setValue(0.0);
                this.setIcon(this.volumeBT, this.volOffIcon, 25);
            } else {
                this.mediaPlayer.setVolume(this.volumeValue);
                this.volumeSD.setValue(this.volumeValue * 100.0);
                this.setIcon(this.volumeBT, this.volOnIcon, 15);
            }
        });
    }

    private void setVolumeSD() {
        this.volumeSD.valueProperty().addListener((ChangeListener)new ChangeListener<Number>(){

            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                PlayerController.this.mediaPlayer.setVolume(newValue.doubleValue() / 100.0);
            }
        });
    }

    protected void updateValues() {
        if (this.processSD != null && this.timeLB != null && this.volumeSD != null && this.volumeBT != null) {
            Platform.runLater((Runnable)new Runnable(){

                @Override
                public void run() {
                    Duration currentTime = PlayerController.this.mediaPlayer.getCurrentTime();
                    PlayerController.this.timeLB.setText(PlayerController.this.formatTime(currentTime, PlayerController.this.duration));
                    PlayerController.this.processSD.setDisable(PlayerController.this.duration.isUnknown());
                    if (!PlayerController.this.processSD.isDisabled() && PlayerController.this.duration.greaterThan(Duration.ZERO) && !PlayerController.this.processSD.isValueChanging()) {
                        PlayerController.this.processSD.setValue(currentTime.toMillis() / PlayerController.this.duration.toMillis() * 100.0);
                    }
                    if (!PlayerController.this.volumeSD.isValueChanging()) {
                        PlayerController.this.volumeSD.setValue((double)((int)Math.round(PlayerController.this.mediaPlayer.getVolume() * 100.0)));
                        if (PlayerController.this.mediaPlayer.getVolume() == 0.0) {
                            PlayerController.this.setIcon(PlayerController.this.volumeBT, PlayerController.this.volOffIcon, 20);
                        } else {
                            PlayerController.this.setIcon(PlayerController.this.volumeBT, PlayerController.this.volOnIcon, 20);
                        }
                    }
                }
            });
        }
    }

    protected String formatTime(Duration elapsed, Duration duration) {
        int intElapsed = (int)Math.floor(elapsed.toSeconds());
        int elapsedHours = intElapsed / 3600;
        int elapsedMinutes = (intElapsed - elapsedHours * 60 * 60) / 60;
        int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60;
        if (duration.greaterThan(Duration.ZERO)) {
            int intDuration = (int)Math.floor(duration.toSeconds());
            int durationHours = intDuration / 3600;
            int durationMinutes = (intDuration - durationHours * 60 * 60) / 60;
            int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60;
            if (durationHours > 0) {
                return String.format("%02d:%02d:%02d / %02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds, durationHours, durationMinutes, durationSeconds);
            }
            return String.format("%02d:%02d / %02d:%02d", elapsedMinutes, elapsedSeconds, durationMinutes, durationSeconds);
        }
        if (elapsedHours > 0) {
            return String.format("%02d:%02d:%02d / %02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
        }
        return String.format("%02d:%02d / %02d:%02d", elapsedMinutes, elapsedSeconds);
    }

    private void setIcon(Button button, String path, int size) {
        Image icon = new Image(path);
        ImageView imageView = new ImageView(icon);
        imageView.setFitWidth((double)size);
        imageView.setFitHeight((double)((int)((double)size * icon.getHeight() / icon.getWidth())));
        button.setGraphic((Node)imageView);
        ColorAdjust colorAdjust = new ColorAdjust();
        button.setOnMousePressed(event -> {
            colorAdjust.setBrightness(0.5);
            button.setEffect((Effect)colorAdjust);
        });
        button.setOnMouseReleased(event -> {
            colorAdjust.setBrightness(0.0);
            button.setEffect((Effect)colorAdjust);
        });
    }

    public MediaPlayer getMediaPlayer() {
        return this.mediaPlayer;
    }

    public void destroy() {
        if (this.mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
            this.mediaPlayer.stop();
        }
        this.mediaPlayer.dispose();
        this.media = null;
        this.mediaPlayer = null;
        System.gc();
    }

    public void setSize(int width, int height) {
        this.currentWidth = width;
        this.currentHeight = height;
        this.setUISuitable();
    }

    private void setUISuitable() {
        this.anchorPane.setPrefSize((double)this.currentWidth, (double)this.currentHeight);
        AnchorPane.setBottomAnchor((Node)this.controlBar, (Double)0.0);
        AnchorPane.setTopAnchor((Node)this.mediaPane, (Double)(((double)this.currentHeight - (double)this.currentWidth * (double)this.mediaHeight / (double)this.mediaWidth - 50.0) / 2.0));
        this.mediaView.setFitWidth((double)this.currentWidth);
        this.mediaView.setFitHeight((double)this.currentWidth * (double)this.mediaHeight / (double)this.mediaHeight);
        this.controlBar.setPrefWidth((double)this.currentWidth);
    }

    public boolean getPopup() {
        return this.popup;
    }
}

